package com.zhaogang.controller;

import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.zhaogang.dto.SaleOrderManageDTO;
import com.zhaogang.entity.Goods;
import com.zhaogang.entity.Page;
import com.zhaogang.entity.SaleAccount;
import com.zhaogang.service.GoodsService;
import com.zhaogang.service.OrderService;

@Controller
@RequestMapping(value="/saleOrder")
public class SaleOrderController {
   @Resource
   private OrderService orderService;
   
   @Resource
   private GoodsService goodsService;
   
   @RequestMapping(value="/saleOrderList")
   public String saleOrderList(){
	   return "redirect:/saleOrder/saleOrderListCon";
   }
   
   //条件查询商户订单
   @RequestMapping(value="/saleOrderListCon")
   public ModelAndView saleOrderListCon(Page page,String goodsName,String state,
		    HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException{
	   ModelAndView view=new ModelAndView();
	   HttpSession session=request.getSession();
	  
	   SaleAccount sale=(SaleAccount) session.getAttribute("saleToken");
	   System.out.println(sale.getId());
	   SaleOrderManageDTO saleOrderDto=new SaleOrderManageDTO();
	   saleOrderDto.setSaleAccountId(sale.getId());
	
	    if(goodsName!=""&&goodsName!=null){
	    	String goodsName1=new String(request.getParameter("goodsName").getBytes("ISO8859-1"),"UTF-8");    //get方法传值
			System.out.println(goodsName1);
			view.addObject("listGoodsName", goodsName1); 
			saleOrderDto.setGoodsName(goodsName1);
	    }
	    if(state!=""&&state!=null){
	    	String state1=new String(request.getParameter("state").getBytes("ISO8859-1"),"UTF-8");
			view.addObject("listState", state1); 
			System.out.println(state1);
			Integer state2=Integer.parseInt(state1);
			saleOrderDto.setState(state2);
	   }
		try{
			 Page p=orderService.findSaleOrderPage1(saleOrderDto, page);
			 
			 System.out.println(p);
			 page.setTotalCount(p.getTotalCount());
			 page.setTotalPageCount(p.getTotalPageCount());
			 List<SaleOrderManageDTO> orderListCon=orderService.querySaleOrderByCon1(saleOrderDto,p);
			 view.addObject("list", orderListCon);
		   }catch(Exception e){
			   e.printStackTrace();
		   }
		  
	   view.setViewName("goods/saleOrder");
	   return view;   
   }
   
   @RequestMapping(value="/goodsInfo", method = RequestMethod.GET)

   public String queryGoodsObject(HttpServletRequest request,Integer id,Character valid,Model model){
	   try{
		   String id1=request.getParameter("id");
		   String valid1=String.valueOf(valid);
		   System.out.println(id1);
		   System.out.println(valid1);
		   Goods goods= goodsService.queryGoodsByGoodsId(id);
		   System.out.println(goods);
		   model.addAttribute("goods", goods);
		   model.addAttribute("valid", valid1);
	   }catch(Exception e){
		   e.printStackTrace();  
	   }
	   return "goods/goodsInfo";
   }
}

